package com.mybase.app.extend.IM;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import com.mybase.app.extend.IM.IMFragmnet.FragmentUserList;
import com.mybase.app.R;
import com.mybase.app.Utils.ToastUtil;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by happy_000 on 2014/12/1.
 */
public class IMActivity extends FragmentActivity {
    private ViewPager viewPager;
    private Handler mHandler;
    private View regiter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.im_home);
        viewPager= (ViewPager) findViewById(R.id.im_viewpager);
        viewPager.setAdapter(myPagerAdapter);
        // 此处直接 hardcode 给 token 赋值，请替换为您自己的 Token。
        String token = "wI6Oxod7ROIy+pN+eqOdsIG9OhrbZXihlVgQputr8xP5WgTIOG0iQu9VgRkbWi8vQbb6urToXre8CX8XKOWzuA==";

        // 连接融云服务器。
        RongIM.connect(token, new RongIMClient.ConnectCallback() {

            @Override
            public void onSuccess(String s) {
                // 此处处理连接成功。
                ToastUtil.toastLonger(IMActivity.this, "Connect:Login successfully.");
            }

            @Override
            public void onError(ErrorCode errorCode) {
                // 此处处理连接错误。
                ToastUtil.toastLonger(IMActivity.this, "Connect:Login failed.");
            }
        });
        mHandler = new Handler();

        // 点击按钮发起聊天会话。
        regiter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mHandler.post(new Runnable() {
                                  @Override
                                  public void run() {
                                      String userId = "user01";
                                      String title = "自问自答";
                                      // 启动聊天会话。
                                      // RongIM.getInstance().startPrivateChat(IMHomeActivity.this, userId, title);
                                      // 启动会话列表
                                      // RongIM.getInstance().startConversationList(IMHomeActivity.this);
                                      //启动客服聊天界面
                                      String customerServiceId = "KEFU12345";
                                      RongIM.getInstance().startCustomerServiceChat(IMActivity.this, customerServiceId, "在线客服");
                                  }
                              }
                );
            }
        });


    }
    private void initFragment(){
        FragmentUserList framgMnetUserList=new FragmentUserList();
    }

    private List<Fragment> fragmentList=new ArrayList<Fragment>(0);
    FragmentPagerAdapter myPagerAdapter= new FragmentPagerAdapter(getSupportFragmentManager()){
        @Override
        public Fragment getItem(int i) {
            return fragmentList.get(i);
        }

        @Override
        public int getCount() {
            return fragmentList.size()>0?fragmentList.size():0;
        }
    };


}
